package com.gmail.dmitryustimov.ms.java7.observer.classic.testperf;

import com.gmail.dmitryustimov.ms.java7.observer.classic.demo.subj10subsc100event10.*;

import java.util.ArrayList;
import java.util.List;

public class TestPerfSubj10Subsc100Event10 {

    private static final int NUM_SUBJECTS = 10;
    private static final int NUM_SUBSCRIBERS = 100;
    private static final int NUM_UPDATES = 100000;
    private static final int NUM_WARM_UP = 3;
    private static final int NUM_TESTS = 10;

    private static final String MESSAGE = "observer_classic, 10 subject, 100 subscriber, 10 event type, took: ";

    public static void main(String[] args) {

        //warm up
        for (int i = 0; i < NUM_WARM_UP; i++) {
            test();
        }

        //tests
        long time = 0;
        for (int i = 0; i < NUM_TESTS; i++) {
            time += test();
        }
        System.out.println("final - " + MESSAGE + time/NUM_TESTS + "ns (" + (time/NUM_TESTS)/1000000 + "ms)");
    }

    private static long test() {
        List<Subject> subjectList = new ArrayList<>();

        for (int i = 0; i < NUM_SUBJECTS; i++) {
            Subject subject = new Subject("" + i);
            subjectList.add(subject);
            for (int j = 0; j < (NUM_SUBSCRIBERS); j++) {
                new Subscriber(subject, "Subscriber" + i + "_" + j);
            }
        }

        long startTime = System.nanoTime();

        for (int k = 0; k < NUM_UPDATES; k++) {
            for (int h = 0; h < NUM_SUBJECTS; h++) {
                Subject subject = subjectList.get(h);

                subject.makeEvent1();
                subject.makeEvent2();
                subject.makeEvent3();
                subject.makeEvent4();
                subject.makeEvent5();
                subject.makeEvent6();
                subject.makeEvent7();
                subject.makeEvent8();
                subject.makeEvent9();
                subject.makeEvent10();
            }
        }

        long estimatedTime = System.nanoTime() - startTime;
        System.out.println(MESSAGE + estimatedTime + "ns");
        return estimatedTime;
    }
}
